{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import norm\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n参考地址： \\nhttps://www.zhihu.com/question/56891433/answer/651340140\\n\\n朗西斯·高尔顿爵士（1822－1911），查尔斯·达尔文的表弟，英格兰维多利亚时代的博学家、人类学家、优生学家、热带探险家、地理学家、发明家、气象学家、统计学家、心理学家和遗传学家。\\n\\n他发明了一个叫做高尔顿钉板的装置，展示了正态分布的产生过程：高尔顿钉板是一种装置，它是一个木盒子，里面均匀分布着若干排钉子。从入口处把小球倒入钉板，最终箱子里面形成的分布近似为高斯分布。弹珠往下滚的时候，撞到钉子就会随机选择往左边走，还是往右边走。一颗弹珠一路滚下来会多次选择方向，最终的分布会接近正态分布。\\n高尔顿钉板有两处细节：\\n\\n顶上只有一处开口：这是要求弹珠的起始状态一致。类比女性身高的例子，就是要求至少物种一致，总不能猪和人一起比较。换成数学用语就是要求同分布。\\n开口位于顶部中央：这倒无所谓，开在别的位置，分布形态不变，只是平移\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "参考地址： \n",
    "https://www.zhihu.com/question/56891433/answer/651340140\n",
    "\n",
    "朗西斯·高尔顿爵士（1822－1911），查尔斯·达尔文的表弟，英格兰维多利亚时代的博学家、人类学家、优生学家、热带探险家、地理学家、发明家、气象学家、统计学家、心理学家和遗传学家。\n",
    "\n",
    "他发明了一个叫做高尔顿钉板的装置，展示了正态分布的产生过程：高尔顿钉板是一种装置，它是一个木盒子，里面均匀分布着若干排钉子。从入口处把小球倒入钉板，最终箱子里面形成的分布近似为高斯分布。弹珠往下滚的时候，撞到钉子就会随机选择往左边走，还是往右边走。一颗弹珠一路滚下来会多次选择方向，最终的分布会接近正态分布。\n",
    "高尔顿钉板有两处细节：\n",
    "\n",
    "顶上只有一处开口：这是要求弹珠的起始状态一致。类比女性身高的例子，就是要求至少物种一致，总不能猪和人一起比较。换成数学用语就是要求同分布。\n",
    "开口位于顶部中央：这倒无所谓，开在别的位置，分布形态不变，只是平移\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Il7ecAAAACXBIWXMAAAsTAAALEwEAmpwYAAAReElEQVR4nO3de6ykdX3H8fdHVqzXsMgJQdh20a5NsFHELdJoja2Rm0mB2liwkYVo1qSQSoKpq03Eakxo6yU1WhKsq9iohHqpW9mIK9EQm6IsiKsLsayIZVeE1bVeaqqFfvvH/I5Ol3Pbc2bmzO7v/Uom88z3+c3Md2ZnPvOc3/PMbKoKSVIfHrPaDUiSJsfQl6SOGPqS1BFDX5I6YuhLUkfWrHYDCznuuONq/fr1q92GJB1Wbr/99u9X1cxc66Y69NevX8/OnTtXuw1JOqwk+c5865zekaSOGPqS1BFDX5I6YuhLUkcMfUnqiKEvSR0x9CWpI4a+JHXE0Jekjhj6ktQRQ1+SOrJo6CdZl+QLSe5KsjvJ61r9LUn2Jbmznc4dus4bk+xJ8s0kZw3Vz261PUm2jOchSZLms5QfXHsYuLKq7kjyZOD2JDvaundX1TuGByc5BbgQeBbwNODzSZ7ZVr8PeCmwF7gtybaqumsUD0SStLhFQ7+qHgAeaMs/SXI3cOICVzkPuL6qfg58O8ke4PS2bk9V3QuQ5Po21tCXpAk5pDn9JOuB5wJfbqXLk+xKsjXJ2lY7Ebh/6Gp7W22++sH3sTnJziQ79+/ffyjtSZIWseTQT/Ik4BPAFVX1Y+Aa4BnAqQz+EnjnKB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gIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "'''\n",
    "用代码实现高尔顿钉板：\n",
    "\n",
    "n个小球\n",
    "箱子的宽度为box_width\n",
    "row_count表示一共有多少行钉子\n",
    "col_count表示一共有多少列钉子\n",
    "问题本质是：给定一个初始状态，执行row_count次随机增减，最终得到的数组满足高斯分布。\n",
    "'''\n",
    "\n",
    "n = 100000  # 小球的个数\n",
    "box_width = 1  # 箱子的宽度\n",
    "row_count = 1000  # 每行钉子的个数\n",
    "col_count = 1000  # 每列钉子的个数\n",
    "\n",
    "a = np.ones(n) * box_width / 2  # 开始时的位置\n",
    "# 模拟小球随机下落\n",
    "for i in range(row_count):\n",
    "    delta = (np.random.randint(0, 2, n) - 0.5) * box_width / col_count\n",
    "    a += delta\n",
    "    # 剔除边界外面的小球\n",
    "    a = np.clip(a, 0, box_width)\n",
    "plt.hist(a, col_count // 2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.49997350999999923, 0.01579871033596962)\n"
     ]
    }
   ],
   "source": [
    "# 小球最后位置的分布 实际上跟 位置 有关\n",
    "# 那么利用极大似然估计就是 想找到 在哪个位置 小球的数量最多\n",
    "# 通过上面的图形实际上可以看出 小球最终落入的位置大致呈现一个正态分布\n",
    "# 正态分布的一组数据实际上是受 平均值、方差（数据波动的幅度）的影响\n",
    "# 正态分布的极大似然估计估计参数 通过公式推导为 平均值 和 方差\n",
    "# 通过 scipy提供的工具，返回该正态分布的极大似然估计参数值 即 平均值 和 方差\n",
    "print(norm.fit(a)) # 从返回的结果中可以看到 最终位置在 0.499974±0.015799 的小球数量最多"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
